<?php

namespace app\beautiful\controller\admin\setting;

use app\beautiful\model\setting\RegionModel;

use app\BaseController;
use app\beautiful\model\setting\TableNumberModel;


class Tablenumber extends BaseController{

    public function list(){
        $limit=$this->request->limit;
        $page=$this->request->page;
        $region_id = $this->request->param('region_id',0);
        $name = $this->request->param('name',null);
        $shop_id=$this->request->shopId;
        $where=[];
        $order['a.id']='desc';
        $where['region_id']=$region_id;
        $where['shop_id']=$shop_id;
        $where['name']=$name;
        $res_data=TableNumberModel::list($where,$order,$page,$limit);
        return $this->success($res_data);
    }
    public function info(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('主键ID错误');
        }
        $where['id']=$id;
        $res_data=TableNumberModel::info($where);
        return $this->success($res_data);
    }
    public function del(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('主键ID错误');
        }
        $is_delete=1;
        $where['id']=$id;
        $info=TableNumberModel::info($where);
        if(empty($info)){
            return $this->error('数据不存在');
        }
        $res=TableNumberModel::up(['is_delete'=>$is_delete],$where);
        if(!$res){
            return $this->error('删除失败');
        }
        return $this->success();
    }
    public function all_del(){
        $ids = $this->request->param('ids');
        if(empty($ids)){
            return $this->error('主键ID错误');
        }
        $is_delete=1;
        foreach ($ids as $id){
            $where['id']=$id;
            $res=TableNumberModel::up(['is_delete'=>$is_delete],$where);
        }
        if(!$res){
            return $this->error('删除失败');
        }
        return $this->success();
    }
    public function effect(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('主键ID错误');
        }
        $is_effect=$this->request->param('is_effect',null);
        if($is_effect===null){
            return $this->error('is_effect参数错误');
        }
        $where['id']=$id;
        $info=TableNumberModel::info($where);
        if(empty($info)){
            return $this->error('数据不存在');
        }
        $res=TableNumberModel::up(['is_effect'=>$is_effect],$where);
        return $this->success();
    }
    public function edit(){
        $id = $this->request->param('id',0);
        $region_id = $this->request->param('region_id',null);
        $name = $this->request->param('name',null);
        $num = $this->request->param('num',null);
        $remark = $this->request->param('remark',null);
        if($id==0){
            return $this->error('主键ID错误');
        }
        $TableNumberModel= new TableNumberModel();
        $info=$TableNumberModel->where(['id'=>$id])->find();
        if(empty($info)) return $this->error('该桌号不存在');
        if($region_id!==null && $region_id!=0){
            $p_info=RegionModel::where(['id'=>$region_id,'is_delete'=>0])->find();
            if(empty($p_info)) return $this->error('该区域不存在或已删除');
        }
        $up=[];
        if($region_id!=null) $up['region_id']=$region_id;
        if($name!=null) $up['name']=$name;
        if($num!=null) $up['num']=$num;
        if($remark!=null) $up['remark']=$remark;
        $where['id']=$id;
        $res_data=TableNumberModel::up($up,$where);
        return $this->success($res_data);
    }
    public function add(){
        $region_id = $this->request->param('region_id',0);
        $is_effect = $this->request->param('is_effect',0);
        $table_data = $this->request->param('table_data','');
        $remark = $this->request->param('remark','');
        if($table_data=='') return $this->error('桌号内容不能为空');//
        $shop_id=$this->request->shopId;
        $TableNumberModel= new TableNumberModel();
        foreach ($table_data as $table){
            $insert=[];
            $s=[];
            $s[]=['name','=',$table['name']];
            $s[]=['is_delete','=',0];
            $s[]=['region_id','=',$region_id];
            $s[]=['shop_id','=',$shop_id];
            $info=$TableNumberModel->where($s)->find();
            if(empty($info)){
                $insert['region_id']=$region_id;
                $insert['is_effect']=$is_effect;
                $insert['shop_id']=$shop_id;
                $insert['remark']=$remark;
                $insert['name']=$table['name'];
                $insert['num']=$table['num'];
                $insert_id=TableNumberModel::insert($insert);
            }
        }
        return $this->success($insert_id);
    }
    public function downzip(){
        $shop_id=$this->request->shopId;
        $where=[];
        $where['shop_id']=$shop_id;
        $table=new TableNumberModel();
        $zip_url=$table->qrcode($where);
        return $this->success($zip_url);
    }
}